<?php
// 处理器 （对 HTML 文档进行处理）

namespace App\Handlers;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\DomCrawler\Crawler;
use Carbon\Carbon;
use Cache;
use DownHtmlHandler;
use App\Jobs\AnalysisSubject;

class TiebaIndexHandler 
{
	
	// 首页处理器 仅抓取主题地址， 放入队列

	private function getContent($filename) 
	{
		return $html = Storage::disk('tieba')->get($filename);
	}

	private function deleteFile($filename)
	{
		return $html = Storage::disk('tieba')->delete($filename);
	}
	/**
	 *
	 * 获取首页文件中的主题地址;
	 */
	public function getSubjectUrl($filename)
	{
		$url = [];
		$crawler = new Crawler($this->getContent($filename));
		foreach( $crawler->filter('li.j_thread_list.clearfix:not(.thread_top)') as $node) {
			$c = new Crawler($node);
    		$url = $c->filter('a.j_th_tit')->attr('href');
    		$url = str_replace('/p/', '', $url);
    		// 推送队列抓取 主题
    		dispatch(new AnalysisSubject($url));
    		// $this->getSubjectInfo($url);
		}
		$this->deleteFile($filename);
	}
	// 获取html文件 保存到本地
	public function saveSubjectContent($tid)
	{
		$down = app(\App\Handlers\DownHtmlHandler::class);
		$filename = $down->getSubject($tid);
		return $filename;
	}
	/**
	 *
	 *	获得主题的详细信息
	 *
	 */
	public function getSubjectInfo($filename)
	{
		$crawler = new Crawler($this->getContent($filename));
		return $firstfloor = $crawler->filter('cc div.d_post_content')->html();
	}
	
	// 获得一个用户的详细信息
	public function getUserInfo()
	{

	}
}